//
//  SNTrackCell.m
//  project
//
//  Created by 圆圆陈 on 2017/12/8.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNTrackCell.h"
#import "UIImageView+WebCache.h"
@implementation SNTrackCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.mapImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15,16, 65,73)];
        self.mapImageView.layer.cornerRadius = FitW(10);
        self.mapImageView.clipsToBounds = YES;
        [self.contentView addSubview:self.mapImageView];
        
        CGFloat width = (HHHWIDTH - self.mapImageView.right-FitW(10))/3;
        self.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.mapImageView.right+ 10,20,screenWidth-90 -100-30,20)];
        self.addressLabel.textColor = UIColorHEX(0x161616);
        self.addressLabel.font = SNFONTMEDIUM(15);
//        self.addressLabel.adjustsFontSizeToFitWidth = YES;
        self.addressLabel.text = @"西藏省-家里蹲市-胡来区";
        self.addressLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.contentView addSubview:self.addressLabel];
        
        self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(screenWidth-100-20,20,90, 20)];
        self.timeLabel.textColor = UIColorHEX(0xB4B4BD);
        self.timeLabel.font = SNFONTREGULAR(10);
        self.timeLabel.adjustsFontSizeToFitWidth = YES;
        self.timeLabel.text = @"2017/10/9 14:22";
        self.timeLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.timeLabel];
//        self.addressLabel.backgroundColor = [UIColor redColor];
//        self.timeLabel.backgroundColor = [UIColor blueColor];
        
        self.right = [[UIImageView alloc] initWithFrame:SNFRAME(self.timeLabel.right +3, 18, 14, 24)];
        self.right.image = [UIImage imageNamed:@"next"];
        [self.contentView addSubview:self.right];
       
        NSArray *titleArr = @[HHH_Text(@"总里程"),HHH_Text(@"总耗时"),HHH_Text(@"消耗大卡")];
        CGFloat wwww  = (screenWidth - 90 -20)/3;
        for (int i=0; i<3; i++) {
            if (i==0) {
                self.disLabel = [[UILabel alloc]initWithFrame:CGRectMake(90 +wwww*i, 49, wwww-5,20)];
                self.disLabel.textColor = UIColorHEX(0x161616);
                self.disLabel.textAlignment = NSTextAlignmentLeft;
                self.disLabel.font = SNFONTMEDIUM(15);
//                self.disLabel.attributedText = [self changeLabelWithText:@"23.55km"];
                [self.contentView addSubview:self.disLabel];
            }else if (i==2){
                self.calLabel =[[UILabel alloc]initWithFrame:CGRectMake(90 +wwww*i, 49, wwww +10,20)];
                self.calLabel.textColor =UIColorHEX(0x161616);
                self.calLabel.textAlignment = NSTextAlignmentLeft;
//                self.calLabel.text = @"1455";
                self.calLabel.font = SNFONTMEDIUM(15);
                [self.contentView addSubview:self.calLabel];
//                self.calLabel.backgroundColor = [UIColor redColor];
            }else{
                self.perLabel = [[UILabel alloc]initWithFrame:CGRectMake(90 +wwww*i, 49, wwww-5,20)];
                self.perLabel.textColor = UIColorHEX(0x161616);
                self.perLabel.textAlignment = NSTextAlignmentLeft;
                self.perLabel.font = SNFONTMEDIUM(15);
//                self.perLabel.attributedText = [self changeLabelWithText:@"14h32m"];
                [self.contentView addSubview:self.perLabel];
            }

            UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(90 +wwww*i,70, wwww -5,20)];
            label.text = titleArr[i];
            label.textColor =UIColorHEX(0x424242);
            label.textAlignment = NSTextAlignmentLeft;
            label.font = SNFONTREGULAR(10);
            [self.contentView addSubview:label];
            
//            label.backgroundColor = [UIColor yellowColor];
//            self.disLabel.backgroundColor = [UIColor grayColor];
//            self.calLabel.backgroundColor = [UIColor grayColor];
//            self.perLabel.backgroundColor = [UIColor grayColor];
        }
        
        self.line = [[UIView alloc]initWithFrame:CGRectMake(15, 105, HHHWIDTH, 0.5)];
        self.line.backgroundColor = UIColorHEX(0xD9D9DD);
        [self.contentView addSubview:self.line];
        self.cellH+=self.line.bottom;
    }
    return self;
}

-(NSMutableAttributedString*) changeLabelWithText:(NSString*)string
{
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    UIFont *font = SNFONT(FitFont(16));
    for (int i = 0; i <string.length; i++) {
        unichar ch = [string characterAtIndex:i];
        NSRange range = {i, 1};
        if (!('0' <= ch && ch <= '9')) {
            [attrString addAttribute:NSFontAttributeName value:font range:range];
        }else{
            [attrString addAttribute:NSFontAttributeName value:SNFONT(FitFont(20)) range:range];
        }
    }
    return attrString;
}
-(void)setInfoWithModel:(TrackModel *)model{
//    NSString *path_document = NSHomeDirectory();
//    NSString *imagePath = [path_document stringByAppendingString:model.imagePath];
//    self.mapImageView.image = [UIImage imageWithPath:imagePath];
    
    if (model.imagePathLocal) {
        self.mapImageView.image = model.imagePathLocal;
    }
    else
    {
        [self.mapImageView sd_setImageWithURL:[NSURL URLWithString:model.imagePath]];
    }
    if ([model.address containsString:@"null"]) {
        model.address = @"";
    }
    
    self.addressLabel.text = model.address;
    NSString *timeStr = [model.startTime stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    self.timeLabel.text = [timeStr substringToIndex:timeStr.length-3];
    NSString *disStr = SN_USER.isMetricSystem?[NSString stringWithFormat:@"%.2fmile",roundf(model.totalDistances*0.0006214*100)/100]:[NSString stringWithFormat:@"%.2fkm",model.totalDistances*1.0/1000];
    self.disLabel.text = disStr;
    
    NSString *calStr = [NSString stringWithFormat:@"%.0fkcal",SN_USER.weight*model.totalDistances*1.0/1000*1.036 *1000];
    self.calLabel.text = calStr;
    NSString *perStr =[NSString stringWithFormat:@"%ldh%ldm",model.sportTotalTime/3600,(model.sportTotalTime/60)%60];
    self.perLabel.text = perStr;
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
